#ifndef DFSTHREAD_H
#define DFSTHREAD_H

#include "mapsquare.h"
#include <QObject>
#include <QWidget>
#include <QThread>
#include <QWaitCondition>
#include <QMutex>

class DfsThread : public QThread
{
    Q_OBJECT
public:
    explicit DfsThread(QWidget *parent = nullptr);
    void init(QVector<QVector<MapSquare*>>& _map, int _r, int _c, int _max_step);

public:
    void pause();
    void resume();
    void oneStep(bool);

protected:
    void run() override;
    bool dfs(int r, int c, int step, int max_step);

signals:
    void updateSquare(int r, int c, MapSquare::STATUS status);
    void dstNotFound();

private:
    QVector<QVector<MapSquare*>>* map; // 这里要不断更改指向, 所以不能用引用
    QVector<QVector<bool>> visited;
    int src_r, src_c, max_step;
    int r_map, c_map;
    static const int dx[4];
    static const int dy[4];
private:
    bool one_step = false;
    bool paused = false;
    QMutex mutex;
    QWaitCondition pauseCond;
};

#endif // DFSTHREAD_H
